<?php

require_once ROOT_APPLICATION_MODULES_ADMIN_FORMS . DS . "Category.php";
require_once ROOT_APPLICATION_MODULES_ADMIN_OBJECTS . DS . 'Category.php';
require_once ROOT_APPLICATION_MODULES_ADMIN_MODELS . DS . 'Categorys.php';

require_once ROOT_APPLICATION_MODULES_ADMIN_MODELS . DS . "Users.php";

class Admin_CategoryController extends Zend_Controller_Action {

    ///////////////////////
    // V A R I A B L E S //
    ///////////////////////
    private $modelCategory;
    private $modelUser;
    
    /////////////////////
    // O V E R I D E S //
    ///////////////////// 
    public function init() {
        
        /* Initialize action controller here */ 
        if (Zend_Auth::getInstance()->getIdentity() == FALSE) {
            $this->_redirect('admin/auth/login');
        }
        
        $layoutPath = APPLICATION_PATH . '/templates/admin/default';
        $option = array('layout' => 'index',
            'layoutPath' => $layoutPath);

        Zend_Layout::startMvc($option);
        
        $this->modelCategory = new End_Models_Categorys();
        $this->modelUser = new End_Models_Users();

        //OTHER 
        $this->currentUser = $this->getInforUser();

        //VIEW 
        $this->view->currentUser = $this->currentUser;
        /* Initialize action controller here */
    }

    ///////////////////
    // U T I L I E S //
    ///////////////////
    private function getInforUser() {
        try {
            $currentUser = Zend_Auth::getInstance()->getIdentity();
            return $currentUser;
        } catch (Zend_Exception $ex) {
            
        }
    }
    
    ////////////////////
    // A C T I O N S  //
    ////////////////////   
    public function addAction() {
        //Create form enter info for a new category
        $form = new Application_Form_Category();

        //Set label for the button
        $form->submit->setLabel('Đồng ý');
        $form->cancel->setLabel('Hủy bỏ');

        //Set client view page form  = $form
        $this->view->form = $form;
        $this->view->bodyCopy = "<p >Nhập các thông tin về chuyên mục vào form dưới đây:</p>";
        $created_date = date('Y-m-d H:i:s');
        //If can get client request 
        if ($this->getRequest()->isPost()) {
            $cancel = $this->getRequest()->getPost('cancel', '0');

            if ($cancel) {
                $this->_helper->redirector('index');
            }
            //Get data from form.
            $formData = $this->getRequest()->getPost();
            if ($form->isValid($formData)) {   //If data is valid 
                //Get data one by one
                $name = $form->getValue('name');
                $slug = $form->getValue('slug');
                $language = $form->getValue('language');

                //Create new Category object from datas
                $category = new Category(null, $name, $slug, null, 0, 0, 0, 0, 0, $created_date, null, 0, $language);

                //Use insert function from Category model
                $this->modelCategory->_insert($category);

                //Move to index page
                $this->_helper->redirector('index');
            } else {
                //Return form
                $form->populate($formData);
            }
        }
    }

    public function deleteAction() {
        if ($this->getRequest()->isPost()) {
            $del = $this->getRequest()->getPost('del');
            if ($del == 'Yes') {
                $category_id = (int) $this->getRequest()->getPost('category_id');
                $categorys = new End_Models_Categorys();
                $categorys->_delete($category_id);
            }
            $this->_helper->redirector('index');
        } else {
            $category_id = $this->_getParam('category_id', 0);
            $this->view->Categorys = Utility_Converter::objectToArray($this->modelCategory->_find($category_id));
        }
    }

    public function editAction() {
        $category_id = $this->_getParam('category_id');
        $form = new Application_Form_Category();
        $form->submit->setLabel('Đồng ý');
        $form->cancel->setLabel('Hủy bỏ');
        $modified_date = date('Y-m-d H:i:s');
        $this->view->form = $form;

        if ($this->getRequest()->isPost()) {
            $cancel = $this->getRequest()->getPost('cancel', 0);
            if ($cancel) {
                $this->_helper->redirector('index');
            }
            $formData = $this->getRequest()->getPost();
            if ($form->isValid($formData)) {

                $name = $form->getValue('name');
                $slug = $form->getValue('slug');
                $language = $form->getValue('language');
                $category = new Category($category_id, $name, $slug, null, 0, 0, 0, 0, 0, null, $modified_date, 0, $language); //dua thong tin vao doi tuong
                //tao doi tuong model voi cac phuong thuc xu li doi tuong o tren
                $this->modelCategory->_update($category);        //xu li doi tuong voi phuong thuc cua model
                $this->_helper->redirector('index');    //chuyen den trang index
            } else {
                $form->populate($formData);
            }
        } else {
            $category_id = $this->_getParam('category_id', 0);
            if ($category_id > 0) {
                $form->populate(Utility_Converter::objectToArray($this->modelCategory->_find($category_id)));
            }
        }
    }

    public function indexAction() {
        $category = $this->modelCategory->fetchAll();
        $this->view->Category = $category;
    }

}

